package org.frv.wispr;

import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.spring.injection.annot.SpringComponentInjector;
import org.apache.wicket.security.swarm.SwarmWebApplication;
import org.apache.wicket.security.hive.config.PolicyFileHiveFactory;
import org.apache.wicket.security.hive.HiveMind;
import org.apache.wicket.WicketRuntimeException;
import org.apache.wicket.Session;
import org.apache.wicket.Request;
import org.apache.wicket.Response;
import org.frv.wispr.web.account.AccountPage;
import org.frv.wispr.web.account.LoginPage;
import org.frv.wispr.web.account.WisprSession;

import java.net.MalformedURLException;

/**
 * Application object for your web application. If you want to run this application without deploying, run the Start class.
 * 
 * @see wicket.myproject.Start#main(String[])
 */
public class WisprApplication extends SwarmWebApplication
{    
    /**
     * Constructor
     */
	public WisprApplication() {
        
    }
	
	public void init() {
        super.init();
        addComponentInstantiationListener(new SpringComponentInjector(this));
	}

    protected void setUpHive() {

        //create factory
        PolicyFileHiveFactory factory = new PolicyFileHiveFactory();
        try
        {
            //this example uses 1 policy file but you can add as many as you like
            factory.addPolicyFile(getServletContext().getResource("/WEB-INF/wispr.hive"));
        }
        catch (MalformedURLException e)
        {
            throw new WicketRuntimeException(e);
        }
        //register factory
        HiveMind.registerHive(getHiveKey(), factory);
        //To change body of implemented methods use File | Settings | File Templates.
    }

    protected Object getHiveKey() {
        return getServletContext().getContextPath();
    }

    /**
	 * TODO: needs to go to an generally authorized page
	 */
	public Class getHomePage() {
		return AccountPage.class;
	}

    public Class getLoginPage() {
        return LoginPage.class;
    }

    @Override
    public Session newSession(Request request, Response response) {
		return new WisprSession(this, request);
	}
}
